package net.entelijan.cobean.examples.showcase;

import java.awt.Container;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import net.entelijan.cobean.examples.showcase.bind.CobeanBindShowcasePanel;
import net.entelijan.cobean.examples.showcase.config.CobeanConfigShowcasePanel;
import net.entelijan.cobean.util.LookAndFeel;
import net.entelijan.scf.ShowcaseDescriptor;
import net.miginfocom.swing.MigLayout;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CobeanShowcase {

	private static Log log = LogFactory.getLog(CobeanShowcase.class);

	public static void main(String... args) {
		LookAndFeel.PLASTIC_XP.use(LookAndFeel.PLASTIC);
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					new CobeanShowcase().open();
				} catch (Exception e) {
					log.error(
							"Cannot open Showcase because: " + e.getMessage(),
							e);
				}
			}
		});
	}

	public void open() throws IOException {
		CobeanShowcasePanel panel = new CobeanShowcasePanel();
		{
			Collection<ShowcaseDescriptor> showcases = loadShowcases("showcase.xml");
			for (ShowcaseDescriptor sc : showcases) {
				try {
					sc.init(panel);
				} catch (Exception e) {
					log.warn("Could not initialize showcase '" + sc
							+ "' because: " + e.getMessage(), e);
				}
			}
		}
		CobeanBindShowcasePanel bindPanel = new CobeanBindShowcasePanel();
		{
			Collection<ShowcaseDescriptor> showcases = loadShowcases("bind-showcase.xml");
			for (ShowcaseDescriptor sc : showcases) {
				try {
					sc.init(bindPanel);
				} catch (Exception e) {
					log.warn("Could not initialize showcase '" + sc
							+ "' because: " + e.getMessage(), e);
				}
			}
		}
		CobeanConfigShowcasePanel configPanel = new CobeanConfigShowcasePanel();
		{
			Collection<ShowcaseDescriptor> showcases = loadShowcases("config-showcase.xml");
			for (ShowcaseDescriptor sc : showcases) {
				try {
					sc.init(configPanel);
				} catch (Exception e) {
					log.warn("Could not initialize showcase '" + sc
							+ "' because: " + e.getMessage(), e);
				}
			}
		}
		final JFrame frame = createFrame(panel, bindPanel, configPanel);
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frame.setVisible(true);
			}
		});
	}

	private JFrame createFrame(JPanel panel, JPanel bindPanel,
			CobeanConfigShowcasePanel configPanel) {
		JFrame f = new JFrame("cobean showcase");
		URL url = getClass().getClassLoader().getResource(
				"logo-small-white.png");
		if (url != null) {
			f.setIconImage(new ImageIcon(url).getImage());
		}
		Container cp = f.getContentPane();
		cp.setLayout(new MigLayout());
		JTabbedPane tp = new JTabbedPane();

		tp.add(configPanel, "Cobean Config");
		tp.add(bindPanel, "Cobean Bind");
		tp.add(panel, "Cobean Core");

		cp.add(tp, "w max, h max, grow");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(800, 800);
		return f;
	}

	/**
	 * @param showcaseResource
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Collection<ShowcaseDescriptor> loadShowcases(
			final String showcaseResource) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				showcaseResource);
		return (Collection<ShowcaseDescriptor>) ctx.getBeansOfType(
				ShowcaseDescriptor.class).values();
	}

}
